package com.beta.elephant.vo;

import lombok.Data;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

/**
 * @Auther: pcc
 * @Date: 2021/11/5 16:27
 * @Description:
 */
@Data
public class BasePageReq implements Serializable {
    //查询限制最大数
    private static Integer MAX_PAGE_SIZE = 2000;
    private static Integer DEFAULT_PAGE_SIZE = 1000;
    private Integer pageSize = 1;
    private Integer currentPage = 0;
    private Integer totalRecored = 0;
    public static final String ASC = "ASC";
    public static final String DESC = "DESC";
    private String column;
    private String columnSort;

    public String getColumn() {
        return this.column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public String getColumSort() {
        if (StringUtils.isEmpty(this.columnSort)) {
            return this.columnSort;
        } else {
            this.columnSort = StringUtils.upperCase(this.columnSort);
            return this.columnSort;
        }
    }

    public void setColumSort(String sort) {
        this.columnSort = sort;
    }

    public void setPageSize(Integer pageSize) {
        if (pageSize == null) {
            this.pageSize = DEFAULT_PAGE_SIZE;
        } else if (pageSize > MAX_PAGE_SIZE) {
            this.pageSize = MAX_PAGE_SIZE;
        } else {
            this.pageSize = pageSize;
        }
    }

    public Integer getCurrentPage() {
        return this.currentPage != null && this.currentPage >= 1 ? this.currentPage : 1;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageNo() {
        return getCurrentPage();
    }

    public void setPageNo(int pageNo) {
        setCurrentPage(pageNo);
    }

    public Integer getPageTotal() {
        if (this.totalRecored == null) {
            return 0;
        } else {
            int tp = this.totalRecored / this.getPageSize();
            if (this.totalRecored % this.getPageSize() > 0) {
                ++tp;
            }

            return tp;
        }
    }

    public Integer getTotalRecored() {
        return this.totalRecored;
    }

    public void setTotalRecored(Integer totalRecored) {
        this.totalRecored = totalRecored;
    }

    public Integer getOffset() {
        return (this.getCurrentPage() - 1) * this.getPageSize();
    }

}
